package com.example.xunai.dto.request.system;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageRequest {
    private Integer page = 1;

    private Integer size = 20;

    private String sortBy;

    private String sortDirection = "DESC";

    public Pageable toPageable() {
        if (sortBy != null && !sortBy.trim().isEmpty()) {
            Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), sortBy);
            return org.springframework.data.domain.PageRequest.of(page - 1, size, sort);
        }
        return org.springframework.data.domain.PageRequest.of(page - 1, size);
    }

    public boolean isValid() {
        return page != null && page > 0 && size != null && size > 0 && size <= 1000;
    }
}